<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Controller;
use think\Db;
use think\db\Query;
use think\Validate;
use app\admin\model\Banner as bannerModel;

class Banner extends controller
{
    public function index()
    {
        //获取当前请求的name变量
        $name = $this->request->param('name');
        //接受所查询的学校的值
        $banner = Db::name('banner')
            ->where(function (Query $query) use ($name) {
                //模糊查询
                if ($name) {
                    $query->where('name', 'like', "%$name%");
                }
            })
            //倒序
            ->order('id DESC')
            //分页 保持页面稳定性 接受全部数据
            ->paginate(3);
        //$member->appends(['mname' => $name]);
        //分页样式
        $page = $banner->render();
        //映射页面 接收返回的值
        $this->assign("page", $page);
        $this->assign("banner", $banner);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = input();
            $rule = [
                'name'  =>  'require|max:10',

            ];
            $msg = [
                'name.require' => '请输入姓名',

            ];
            $validate = Validate::make($rule, $msg);
            $rs = $validate->check($data);
            if ($rs !== true) {
                return $this->error($validate->getError());
            }
            $add = Db::name('banner')->insert($data);
            if ($add) {
                return $this->success('添加成功','admin/banner/index');
            } else {
                return $this->error('添加失败');
            }
        }
        //显示页面
        return $this->fetch('publish');
    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $data = input();
            $rule = [
                'image'  =>  'require',

            ];
            $msg = [
                'image.require' => '图片不能为空',

            ];
            $validate = Validate::make($rule, $msg);
            $rs = $validate->check($data);
            if ($rs !== true) {
                return $this->error($validate->getError());
            }
            $add = Db::name('banner')->update($data,['id'=>$data['id']]);
            if ($add) {
                return $this->success('修改成功','admin/banner/index');
            } else {
                return $this->error('修改失败');
            }
        }
        //halt($id);
        $banner = bannerModel::get($id);
        //显示页面
        return $this->fetch('edit',['banner'=>$banner]);
    }

    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('banner')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                //addlog($id);//写入日志
                return $this->success('删除成功','admin/banner/index');
            }
        }
    }


}
